package com.gxuwz.attend.service.student;


import com.gxuwz.attend.dao.student.LeaveDao;
import com.gxuwz.attend.entity.Leave;
import com.gxuwz.attend.tools.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LeaveService {
    @Autowired

    private LeaveDao leaveDao;



    public String applyLeave(String courseID, String reason, int daynum, String stuNo) {
        // 生成请假单号
        String leaveID = DateUtil.generateLeaveID();
        String applytime = String.valueOf(new Date()); // 当前时间
        String status = "0"; // 初始状态：未审核

        // 创建 Leave 对象
        Leave leave = new Leave();
        leave.setLeaveID(leaveID);
        leave.setCourseID(courseID);
        leave.setReason(reason);
        leave.setDaynum(daynum);
        leave.setStuNo(stuNo);
        leave.setApplytime(applytime);
        leave.setAudittime(null);
        leave.setStatus(status);
        leave.setOpinion("未审核");

        // 将请假单信息保存到数据库
        int result = leaveDao.insertLeave(leave);

        // 返回结果信息
        if (result > 0) {
            return "success";
        } else {
            return "error";
        }
    }


    public List<Leave> getLeaves(String stuID){
        return leaveDao.getLeaves(stuID);
    }


    public boolean deleteLeave(String leaveID){
        int rs = leaveDao.deleteLeave(leaveID);
        return rs > 0;
    }
}

